package com.drodds.game;

import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.content.Context;
import android.graphics.drawable.StateListDrawable;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.ImageButton;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.graphics.Color;
import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Handler;

public class GameUI {
	private static final float absResultsW = 0.35f;
	private static final float absResultsH = 0.35f;
	private static final float absResultsX = 0.50f;
	private static final float absResultsY = 0.50f;
	
	private UIWidgetDock dock;
	private RelativeLayout layout;
	private ImageButton spinButton;
	private Context context;
	private StateListDrawable startBtnStates;
	private int lastResult = 0;
	private int resultRange = 0;
	private ImageView resultBG;
	private TextView resultText;
//	private Handler uiHandler;
//	private Runnable uiRunnable;
	
	public GameUI(Handler handler) {
/*		uiHandler = handler;
		uiRunnable = new Runnable() {
			@Override
			public void run() {
				//layout.removeView(resultBG);
				//layout.removeView(resultText);
				resultBG.setAlpha(0.0f);
				resultText.setAlpha(0.0f);
			}
		};
*/
	}
	
	public void init(RelativeLayout l) {
		context = l.getContext();
		layout = l;
		dock = new UIWidgetDock();
		dock.init(context);
		spinButton = initSpinButton();
		dock.push(spinButton);
		resultBG = new ImageView(context);
		resultText = new TextView(context);
		layout.addView(spinButton);
		layout.addView(resultBG);
		layout.addView(resultText);
		
		onSurfaceChanged(context.getResources().getDisplayMetrics().widthPixels, context.getResources().getDisplayMetrics().heightPixels);
	}
	
	@SuppressLint("NewApi")
	private ImageButton initSpinButton() {
		startBtnStates = new StateListDrawable();
		startBtnStates.addState(new int[]{android.R.attr.state_pressed}, context.getResources().getDrawable(R.drawable.startbutton_down));
		startBtnStates.addState(new int[]{android.R.attr.state_focused}, context.getResources().getDrawable(R.drawable.startbutton_hover));
		startBtnStates.addState(new int[]{}, context.getResources().getDrawable(R.drawable.startbutton_up));
	
		ImageButton btn = new ImageButton(context);
		btn.setScaleType(ScaleType.FIT_XY);
		btn.setAdjustViewBounds(true);
		btn.setImageDrawable(startBtnStates);
		if (android.os.Build.VERSION.SDK_INT >= 16) {
			btn.setBackground(null);
		}
		else {
			btn.setBackgroundColor(Color.TRANSPARENT);
		}
        btn.setId(R.id.start_btn);
        
        btn.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		((MainActivity)context).onStartClicked();
        	}
        });
        
        return btn;
	}
	
	public void onSurfaceChanged(int newW, int newH) {
		dock.updatePos();
		
		/*
		 * update spin button
		 */
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)(spinButton.getLayoutParams());
		params.width = dock.getViewBounds(spinButton).width();
		params.height = dock.getViewBounds(spinButton).height();
		params.leftMargin = dock.getViewBounds(spinButton).left;
		params.topMargin = dock.getViewBounds(spinButton).top;
		spinButton.setLayoutParams(params);
	}
	
	private void ShowResult() {
		float pctOfRange = (float)lastResult / (float)resultRange;
		if (lastResult == 1) {
			resultBG.setImageDrawable(context.getResources().getDrawable(R.drawable.result_display_background0));
		}
		else if (pctOfRange < 0.10) {
			resultBG.setImageDrawable(context.getResources().getDrawable(R.drawable.result_display_background1));
		}
		else if (pctOfRange < 0.50) {
			resultBG.setImageDrawable(context.getResources().getDrawable(R.drawable.result_display_background2));
		}
		else {
			resultBG.setImageDrawable(context.getResources().getDrawable(R.drawable.result_display_background3));
		}
		
		resultText.setText(Integer.toString(lastResult));
		resultText.setTextColor(Color.WHITE);
		resultText.setTextSize(60.0f);
		resultText.setGravity(Gravity.CENTER);
		
		int bgWidth = Math.round(context.getResources().getDisplayMetrics().widthPixels * absResultsW);
		int bgHeight = Math.round(context.getResources().getDisplayMetrics().heightPixels * absResultsH);
		int bgLeft = Math.round(context.getResources().getDisplayMetrics().widthPixels * absResultsX - bgWidth / 2);
		int bgTop = Math.round(context.getResources().getDisplayMetrics().heightPixels * absResultsY - bgHeight / 2);
		
		RelativeLayout.LayoutParams bgParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		bgParams.width = bgWidth;
		bgParams.height = bgHeight;
		bgParams.leftMargin = bgLeft;
		bgParams.topMargin = bgTop;
		resultBG.setLayoutParams(bgParams);
		
		RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		textParams.width = bgWidth;
		textParams.height = bgHeight;
		textParams.leftMargin = bgLeft;
		textParams.topMargin = bgTop;
		resultText.setLayoutParams(textParams);
		
		Animation fade = AnimationUtils.loadAnimation(context, R.anim.fade_results);
		fade.setFillAfter(true);
		resultText.startAnimation(fade);
		resultBG.startAnimation(fade);
	}
	
	public Rect getDockBounds() {
		onSurfaceChanged(context.getResources().getDisplayMetrics().widthPixels, context.getResources().getDisplayMetrics().heightPixels);
		return dock.getBounds();
	}
	
	public void giveResults(int res, int resRange) {
		if (res != lastResult && res != 0) {
			resultRange = resRange;
			lastResult = res;
			ShowResult();
		}
	}
	
	public void StoppedSpinning() {
		lastResult = -1;
	}
}
